package com.bawei.property.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import com.bawei.property.databinding.ItemParkApplyBinding
import com.bawei.property.entity.ParkApplyValues
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import java.sql.Date
import java.text.SimpleDateFormat

/**
 * Author: Jason Zhang
 * Date: 2024/5/31
 * Desc: 车位申请列表数据适配器
 */
class ParkApplyListAdapter constructor(private val layoutId: Int): BaseQuickAdapter<ParkApplyValues, ParkApplyListAdapter.ItemViewHolder>(layoutId) {

     class ItemViewHolder constructor(val binding: ItemParkApplyBinding): BaseViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        var binding = DataBindingUtil.inflate<ItemParkApplyBinding>(LayoutInflater.from(parent.context), layoutId, parent, false)
        return ItemViewHolder(binding)
    }

    override fun convert(holder: ItemViewHolder, item: ParkApplyValues) {
        //头部背景颜色
        when(item.type){
            0 -> item.color = Color.parseColor("#A8E067")
            1 -> item.color = Color.parseColor("#e2ae73")
            2 -> item.color = Color.parseColor("#FF6B6B")
        }
        //数据兼容处理，可能由于脏数据导致的问题
        if (!isValidDate(item.applyTime)){
            if (item.applyTime.length > 10){
                item.applyTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date(item.applyTime.toLong() / 1000L)).toString()
            }else{
                item.applyTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date(item.applyTime.toLong() * 1000L)).toString()
            }
        }
        holder.binding.entity = item
    }

    /**
     * 时间格式判断
     */
    private fun isValidDate(dateStr: String): Boolean {
        val regex = "^(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})$"
        return dateStr.matches(regex.toRegex())
    }


}